package com.lzx.applib.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzx.applib.R;


/**
 * Created by Administrator on 16/11/07.
 */

public class TopBar extends LinearLayout {

    View titleLayout;
    TextView title;
    ImageView titleImg;

    View leftLayout;
    TextView leftText;
    ImageView leftImg;

    View rightLayout;
    TextView rightText;
    ImageView rightImg;

    View divider;

    public TopBar(Context context) {
        this(context, null);
    }

    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        findView();

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
        initView(array);
        array.recycle();
    }

    private void findView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.widget_topbar, this, true);

        titleLayout = view.findViewById(R.id.title_layout);
        title = (TextView) view.findViewById(R.id.title);
        titleImg = (ImageView) view.findViewById(R.id.title_img);

        leftLayout = view.findViewById(R.id.left_layout);
        leftText = (TextView) view.findViewById(R.id.left_text);
        leftImg = (ImageView) view.findViewById(R.id.left_img);

        rightLayout = view.findViewById(R.id.right_layout);
        rightText = (TextView) view.findViewById(R.id.right_text);
        rightImg = (ImageView) view.findViewById(R.id.right_img);

        divider = view.findViewById(R.id.divider);
    }

    private void initView(TypedArray array) {
        int leftImgRes = array.getResourceId(R.styleable.TopBar_leftImageRes, 0);
        int leftTextRes = array.getResourceId(R.styleable.TopBar_leftTextRes, 0);
        if (leftImgRes != 0) {
            leftImg.setImageResource(leftImgRes);
        }
        if (leftTextRes != 0){
            leftText.setText(leftTextRes);
        }

        if (leftImgRes!=0 || leftTextRes!=0) {
            setLeftOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((Activity) getContext()).finish();
                }
            });
        }

        int rightImgRes = array.getResourceId(R.styleable.TopBar_rightImageRes, 0);
        int rightTextRes = array.getResourceId(R.styleable.TopBar_rightTextRes, 0);
        if (rightImgRes != 0) {
            rightImg.setImageResource(rightImgRes);
        }
        if (rightTextRes != 0){
            rightText.setText(rightTextRes);
        }

        int titleRes = array.getResourceId(R.styleable.TopBar_titleRes, 0);
        int titleTextColor = array.getColor(R.styleable.TopBar_titleTextColor, Color.BLACK);
        int titleTextSize = array.getDimensionPixelSize(R.styleable.TopBar_titleTextSize, 24);
        title.setTextColor(titleTextColor);
        if (titleRes != 0) {
            title.setText(titleRes);
            title.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleTextSize);
        }

        int titleImageRes = array.getResourceId(R.styleable.TopBar_titleImageRes, 0);
        if (titleImageRes != 0) {
            titleImg.setImageResource(titleImageRes);
        }

        boolean showDivider = array.getBoolean(R.styleable.TopBar_showDivider, true);
        divider.setVisibility(showDivider ? View.VISIBLE : View.GONE);
    }

    public TopBar setTitle(String text) {
        title.setText(text);
        return this;
    }

    public TopBar setTitle(int resId) {
        return setTitle(getResources().getString(resId));
    }

    public TopBar setTitleImage(int resId) {
        titleImg.setImageResource(resId);
        return this;
    }

    public TopBar setTitleOnClickListener(OnClickListener clickListener) {
        titleLayout.setOnClickListener(clickListener);
        return this;
    }

    public TopBar setLeftImage(int resId) {
        leftImg.setImageResource(resId);
        leftLayout.setVisibility(View.VISIBLE);
        return this;
    }

    public TopBar setLeftText(int resId) {
        return setLeftText(getResources().getString(resId));
    }

    public TopBar setLeftText(String text) {
        leftText.setText(text);
        leftLayout.setVisibility(View.VISIBLE);
        return this;
    }

    public TopBar setLeftOnClickListener(OnClickListener clickListener) {
        leftLayout.setOnClickListener(clickListener);
        if (clickListener == null) {
            leftLayout.setVisibility(View.GONE);
        }
        return this;
    }

    public TopBar setRightImage(int resId) {
        rightImg.setImageResource(resId);
        rightLayout.setVisibility(View.VISIBLE);
        return this;
    }

    public TopBar setRightText(int resId) {
        return setRightText(getResources().getString(resId));
    }

    public TopBar setRightText(String text) {
        rightText.setText(text);
        rightLayout.setVisibility(View.VISIBLE);
        return this;
    }

    public TopBar setRightOnClickListener(OnClickListener clickListener) {
        rightLayout.setOnClickListener(clickListener);
        if (clickListener == null) {
            rightLayout.setVisibility(View.GONE);
        } else {
            rightLayout.setVisibility(View.VISIBLE);
        }
        return this;
    }
}
